<?php

namespace SLN0002\Service;

use Home\Common\FIdConst;
use Home\DAO\OrgDAO;
use Home\Service\BizlogService;
use Home\Service\PinyinService;
use Home\Service\PSIBaseExService;
use SLN0002\DAO\SubjectDAO;

/**
 * 会计科目 Service
 *
 * @author PSI
 * @copyright 2015 - present
 * @license GPL v3
 */
class SubjectService extends PSIBaseExService
{
  private $LOG_CATEGORY = "会计科目";

  /**
   * 返回所有的公司列表
   *
   * @return array
   */
  public function companyList()
  {
    if ($this->isNotOnline()) {
      return $this->emptyResult();
    }

    $params = [
      "loginUserId" => $this->getLoginUserId(),
      "fid" => FIdConst::GL_SUBJECT
    ];

    $dao = new OrgDAO($this->db());
    return $dao->getCompanyExList($params);
  }

  /**
   * 某个公司的科目码列表
   *
   * @param array $params
   * @return array
   */
  public function subjectList($params)
  {
    if ($this->isNotOnline()) {
      return $this->emptyResult();
    }

    $dao = new SubjectDAO($this->db());
    return $dao->subjectList($params);
  }

  /**
   * 初始国家标准科目
   */
  public function initStandardSubject($params)
  {
    if ($this->isNotOnline()) {
      return $this->notOnlineError();
    }

    $params["dataOrg"] = $this->getLoginUserDataOrg();

    $db = $this->db();
    $db->startTrans();

    $dao = new SubjectDAO($db);
    $rc = $dao->initStandardSubject($params, new PinyinService());
    if ($rc) {
      $db->rollback();
      return $rc;
    }

    // 记录业务日志
    $companyName = $params["companyName"];
    $log = "为[{$companyName}]初始化国家标准会计科目";
    $bs = new BizlogService($db);
    $bs->insertBizlog($log, $this->LOG_CATEGORY);

    $db->commit();

    // 创建实际的总账数据库表
    // 因为MySQL中DDL是自动事务，所以需要写在上面的事务之外
    $companyId = $params["id"];
    $dao->createGlTableForStandardSubject($companyId);

    return $this->ok();
  }

  /**
   * 新增或编辑会计科目
   *
   * @param array $params
   * @return array
   */
  public function editSubject($params)
  {
    if ($this->isNotOnline()) {
      return $this->notOnlineError();
    }

    $id = $params["id"];

    $params["dataOrg"] = $this->getLoginUserDataOrg();

    $db = $this->db();
    $db->startTrans();

    $dao = new SubjectDAO($db);
    if ($id) {
      // 编辑
      $rc = $dao->updateSubject($params);
      if ($rc) {
        $db->rollback();
        return $rc;
      }
    } else {
      // 新增
      $rc = $dao->addSubject($params);
      if ($rc) {
        $db->rollback();
        return $rc;
      }
      $id = $params["id"];
    }

    // 记录业务日志
    $log = $params["log"];
    $bs = new BizlogService($db);
    $bs->insertBizlog($log, $this->LOG_CATEGORY);

    $db->commit();

    return $this->ok($id);
  }

  /**
   * 上级科目字段 - 查询数据
   *
   * @param string $queryKey
   */
  public function queryDataForParentSubject($queryKey, $companyId)
  {
    if ($this->isNotOnline()) {
      return $this->emptyResult();
    }

    $dao = new SubjectDAO($this->db());
    return $dao->queryDataForParentSubject($queryKey, $companyId);
  }

  /**
   * 某个科目的详情
   *
   * @param array $params
   */
  public function subjectInfo($params)
  {
    if ($this->isNotOnline()) {
      return $this->emptyResult();
    }

    $dao = new SubjectDAO($this->db());
    return $dao->subjectInfo($params);
  }

  /**
   * 删除科目
   *
   * @param array $params
   */
  public function deleteSubject($params)
  {
    if ($this->isNotOnline()) {
      return $this->notOnlineError();
    }

    $db = $this->db();

    $db->startTrans();
    $dao = new SubjectDAO($db);

    $rc = $dao->deleteSubject($params);
    if ($rc) {
      $db->rollback();
      return $rc;
    }

    // 记录业务日志
    $code = $params["code"];
    $log = "删除科目: $code";
    $bs = new BizlogService($db);
    $bs->insertBizlog($log, $this->LOG_CATEGORY);

    $db->commit();

    return $this->ok();
  }

  /**
   * 初始化科目的标准账样
   *
   * @param array $params
   */
  public function initFmt($params)
  {
    if ($this->isNotOnline()) {
      return $this->notOnlineError();
    }

    $db = $this->db();

    $db->startTrans();
    $dao = new SubjectDAO($db);

    $params["dataOrg"] = $this->getLoginUserDataOrg();
    $rc = $dao->initFmt($params);
    if ($rc) {
      $db->rollback();
      return $rc;
    }

    // 记录业务日志
    $log = $params["log"];
    $bs = new BizlogService($db);
    $bs->insertBizlog($log, $this->LOG_CATEGORY);

    $db->commit();

    // 新建账样，创建数据库中的实际物理表
    // 因为MySQL的DDL自动提交事务，所以该代码在创建元数据之后执行
    // 也不需要启动事务

    $rc = $dao->createTableForInitFmt($params);
    if ($rc) {
      return $rc;
    }

    return $this->ok();
  }

  /**
   * 某个科目的账样属性
   *
   * @param array $params
   */
  public function fmtPropList($params)
  {
    if ($this->isNotOnline()) {
      return $this->emptyResult();
    }

    $dao = new SubjectDAO($this->db());
    return $dao->fmtPropList($params);
  }

  /**
   * 某个科目的账样字段列表
   *
   * @param array $params
   */
  public function fmtColsList($params)
  {
    if ($this->isNotOnline()) {
      return $this->emptyResult();
    }

    $dao = new SubjectDAO($this->db());
    return $dao->fmtColsList($params);
  }

  /**
   * 新增或编辑账样字段
   */
  public function editFmtCol($params)
  {
    if ($this->isNotOnline()) {
      return $this->notOnlineError();
    }

    $id = $params["id"];

    if ($this->isDemo()) {
      $n = $id ? "编辑" : "新建";
      return $this->bad("演示环境下，不能{$n}账样列");
    }

    $db = $this->db();

    $db->startTrans();
    $dao = new SubjectDAO($db);

    $log = null;
    $isModify = false;
    if ($id) {
      // 编辑账样字段
      $isModify = true;
      $rc = $dao->updateFmtCol($params);
      if ($rc) {
        $db->rollback();
        return $rc;
      }
    } else {
      // 新增账样字段
      $rc = $dao->addFmtCol($params);
      if ($rc) {
        $db->rollback();
        return $rc;
      }

      $id = $params["id"];
    }

    // 记录业务日志
    $log = $params["log"];
    $bs = new BizlogService($db);
    $bs->insertBizlog($log, $this->LOG_CATEGORY);

    $db->commit();

    if (!$isModify) {
      // 新建账样列，同时创建数据库表字段
      $rc = $dao->createTableColForFMT($params);
      if ($rc) {
        return $rc;
      }
    }

    return $this->ok($id);
  }

  /**
   * 获得某个账样字段的详情
   */
  public function fmtColInfo($params)
  {
    if ($this->isNotOnline()) {
      return $this->emptyResult();
    }

    $dao = new SubjectDAO($this->db());
    return $dao->fmtColInfo($params);
  }

  /**
   * 删除某个账样字段
   */
  public function deleteFmtCol($params)
  {
    if ($this->isNotOnline()) {
      return $this->notOnlineError();
    }

    $db = $this->db();

    $db->startTrans();
    $dao = new SubjectDAO($db);

    $rc = $dao->deleteFmtCol($params);
    if ($rc) {
      $db->rollback();
      return $rc;
    }

    // 记录业务日志
    $caption = $params["caption"];
    $code = $params["subjectCode"];
    $accNumber = $params["accNumber"];
    $log = "删除科目[{$code}]账样[账样号 = {$accNumber}]的字段: $caption";
    $bs = new BizlogService($db);
    $bs->insertBizlog($log, $this->LOG_CATEGORY);

    $db->commit();

    return $this->ok();
  }

  /**
   * 某个账样所有字段 - 设置字段显示次序用
   */
  public function fmtGridColsList($params)
  {
    if ($this->isNotOnline()) {
      return $this->emptyResult();
    }

    $dao = new SubjectDAO($this->db());
    return $dao->fmtGridColsList($params);
  }

  /**
   * 编辑账样字段的显示次序
   */
  public function editFmtColShowOrder($params)
  {
    if ($this->isNotOnline()) {
      return $this->notOnlineError();
    }

    $db = $this->db();

    $db->startTrans();
    $dao = new SubjectDAO($db);

    $rc = $dao->editFmtColShowOrder($params);
    if ($rc) {
      $db->rollback();
      return $rc;
    }

    // 记录业务日志
    $code = $params["subjectCode"];
    $log = "编辑科目[{$code}]账样字段的显示次序";
    $bs = new BizlogService($db);
    $bs->insertBizlog($log, $this->LOG_CATEGORY);

    $db->commit();

    return $this->ok();
  }

  /**
   * 选择值来源的引用列 - 查询表
   */
  public function queryTablesForColRef($params)
  {
    if ($this->isNotOnline()) {
      return $this->emptyResult();
    }

    $dao = new SubjectDAO($this->db());
    return $dao->queryTablesForColRef($params);
  }

  /**
   * 科目自定义字段查询数据
   */
  public function queryDataForSubjectField($params)
  {
    if ($this->isNotOnline()) {
      return $this->emptyResult();
    }

    $dao = new SubjectDAO($this->db());
    return $dao->queryDataForSubjectField($params);
  }

  /**
   * 一键生成数量金额三栏账样列
   */
  public function genCPMFields($params)
  {
    if ($this->isNotOnline()) {
      return $this->notOnlineError();
    }

    $db = $this->db();

    $db->startTrans();
    $dao = new SubjectDAO($db);

    $rc = $dao->genCPMFields($params);
    if ($rc) {
      $db->rollback();
      return $rc;
    }

    // 记录业务日志
    $log = $params["log"];
    $bs = new BizlogService($db);
    $bs->insertBizlog($log, $this->LOG_CATEGORY);

    $db->commit();

    // 创建实际数据库表字段
    $rc = $dao->createDbFieldsForCPM($params);
    if ($rc) {
      return $rc;
    }

    return $this->ok();
  }

  /**
   * 初始化标准进销存账样
   */
  public function initStandardFmtForSLN0001($params)
  {
    if ($this->isNotOnline()) {
      return $this->notOnlineError();
    }

    $params["dataOrg"] = $this->getLoginUserDataOrg();
    $params["pinyinService"] = new PinyinService();

    $db = $this->db();

    $db->startTrans();
    $dao = new SubjectDAO($db);

    $rc = $dao->initStandardFmtForSLN0001($params);
    if ($rc) {
      $db->rollback();
      return $rc;
    }

    // 记录业务日志
    $log = $params["log"];
    $bs = new BizlogService($db);
    $bs->insertBizlog($log, $this->LOG_CATEGORY);

    $db->commit();

    // 创建数据库表
    // 因为MySQL中DDL是自动事务，所以需要写在上面的事务之外
    $companyId = $params["companyId"];
    $vatType = $params["vatType"];
    if (!in_array($vatType, [1, 2])) {
      return $this->bad("vatType参数错误");
    }

    // 创建总账表
    $dao->createGlTableForStandardSubject($companyId);

    // 科目1001 - 库存现金
    $id1001 = $params["id1001"];
    $rc = $this->createTable_SLN0001_1001($dao, $companyId, $id1001);
    if ($rc) {
      return $rc;
    }

    // 科目1002 - 银行存款
    $id1002 = $params["id1002"];
    $rc = $this->createTable_SLN0001_1002($dao, $companyId, $id1002);
    if ($rc) {
      return $rc;
    }

    // 科目1122 - 应收账款
    $id1122 = $params["id1122"];
    $rc = $this->createTable_SLN0001_1122($dao, $companyId, $id1122);
    if ($rc) {
      return $rc;
    }

    // 科目1123 - 预付账款
    $id112301 = $params["id112301"];
    $rc = $this->createTable_SLN0001_1123($dao, $companyId, $id112301);
    if ($rc) {
      return $rc;
    }

    // 科目1402 - 在途物资
    $id1402 = $params["id1402"];
    $rc = $this->createTable_SLN0001_1402($dao, $companyId, $id1402);
    if ($rc) {
      return $rc;
    }

    // 科目1405 - 库存商品
    $id1405 = $params["id1405"];
    $rc = $this->createTable_SLN0001_1405($dao, $companyId, $id1405);
    if ($rc) {
      return $rc;
    }

    // 科目2202 - 应付账款
    $id220201 = $params["id220201"];
    $id220202 = $params["id220202"];
    $rc = $this->createTable_SLN0001_2202($dao, $companyId, $id220201, $id220202);
    if ($rc) {
      return $rc;
    }

    // 科目2221 - 应交税费
    $rc = null;
    if ($vatType == 1) {
      $id22210101 = $params["id22210101"];
      $id22210102 = $params["id22210102"];
      $id222102 = $params["id222102"];
      $rc = $this->createTable_SLN0001_2221_general($dao, $companyId, $id22210101, $id22210102, $id222102);
    } else {
      $id222101 = $params["id222101"];
      $rc = $this->createTable_SLN0001_2221_samll($dao, $companyId, $id222101);
    }
    if ($rc) {
      return $rc;
    }

    // 科目4103 - 本年利润
    $id4103 = $params["id4103"];
    $rc = $this->createTable_SLN0001_4103($dao, $companyId, $id4103);
    if ($rc) {
      return $rc;
    }

    // 科目6001 - 主营业务收入
    $id6001 = $params["id6001"];
    $rc = $this->createTable_SLN0001_6001($dao, $companyId, $id6001);
    if ($rc) {
      return $rc;
    }

    // 科目6401 - 主营业务成本
    $id6401 = $params["id6401"];
    $rc = $this->createTable_SLN0001_6401($dao, $companyId, $id6401);
    if ($rc) {
      return $rc;
    }

    // 科目6601 - 销售费用
    $id6601 = $params["id6601"];
    $rc = $this->createTable_SLN0001_6601($dao, $companyId, $id6601);
    if ($rc) {
      return $rc;
    }

    // 科目6602 - 管理费用
    $id6602 = $params["id6602"];
    $rc = $this->createTable_SLN0001_6602($dao, $companyId, $id6602);
    if ($rc) {
      return $rc;
    }

    // 科目6603 - 财务费用
    $id6603 = $params["id6603"];
    $rc = $this->createTable_SLN0001_6603($dao, $companyId, $id6603);
    if ($rc) {
      return $rc;
    }

    // 操作成功
    return $this->ok();
  }

  /**
   * 标准进销存 - 创建，科目1001 - 库存现金，相关的数据库表
   */
  private function createTable_SLN0001_1001($dao, $companyId, $id1001)
  {
    $createParams = [
      "companyId" => $companyId,
      "id" => $id1001,
    ];
    $rc = $dao->createTableForInitFmt($createParams);
    if ($rc) {
      return $rc;
    }

    // 操作成功
    return null;
  }

  /**
   * 标准进销存 - 创建，科目1002 - 银行存款，相关的数据库表
   */
  private function createTable_SLN0001_1002($dao, $companyId, $id1002)
  {
    $createParams = [
      "companyId" => $companyId,
      "id" => $id1002,
    ];
    $rc = $dao->createTableForInitFmt($createParams);
    if ($rc) {
      return $rc;
    }

    // 科目1002的银行账号字段
    $createParams = [
      "companyId" => $companyId,
      "subjectCode" => "1002",
      "fieldName" => "x_bank",
      "fieldType" => 1,
      "voucherInput" => 2,
    ];
    $rc = $dao->createTableColForFMT($createParams);
    if ($rc) {
      return $rc;
    }

    // 操作成功
    return null;
  }

  /**
   * 标准进销存 - 创建，科目1122 - 应收账款，相关的数据库表
   */
  private function createTable_SLN0001_1122($dao, $companyId, $id1122)
  {
    $createParams = [
      "companyId" => $companyId,
      "id" => $id1122,
    ];
    $rc = $dao->createTableForInitFmt($createParams);
    if ($rc) {
      return $rc;
    }

    // 科目1122的客户字段
    $createParams = [
      "companyId" => $companyId,
      "subjectCode" => "1122",
      "fieldName" => "x_customer",
      "fieldType" => 1,
      "voucherInput" => 2,
    ];
    $rc = $dao->createTableColForFMT($createParams);
    if ($rc) {
      return $rc;
    }

    // 操作成功
    return null;
  }

  /**
   * 标准进销存 - 创建，科目1123 - 预付账款，相关的数据库表
   */
  private function createTable_SLN0001_1123($dao, $companyId, $id112301)
  {
    // 科目112301
    $createParams = [
      "companyId" => $companyId,
      "id" => $id112301,
    ];
    $rc = $dao->createTableForInitFmt($createParams);
    if ($rc) {
      return $rc;
    }

    // 科目112301的供应商字段
    $createParams = [
      "companyId" => $companyId,
      "subjectCode" => "112301",
      "fieldName" => "x_supplier",
      "fieldType" => 1,
      "voucherInput" => 2,
    ];
    $rc = $dao->createTableColForFMT($createParams);
    if ($rc) {
      return $rc;
    }

    // 操作成功
    return null;
  }

  /**
   * 标准进销存 - 创建，科目1402 - 在途物资，相关的数据库表
   */
  private function createTable_SLN0001_1402($dao, $companyId, $id1402)
  {
    $createParams = [
      "companyId" => $companyId,
      "id" => $id1402,
    ];
    $rc = $dao->createTableForInitFmt($createParams);
    if ($rc) {
      return $rc;
    }

    // 科目1402的物料字段
    $createParams = [
      "companyId" => $companyId,
      "subjectCode" => "1402",
      "fieldName" => "x_goods",
      "fieldType" => 1,
      "voucherInput" => 2,
    ];
    $rc = $dao->createTableColForFMT($createParams);
    if ($rc) {
      return $rc;
    }

    // 科目1402的数量金额三栏字段
    $createParams = [
      "companyId" => $companyId,
      "subjectCode" => "1402",
    ];
    $rc = $dao->createDbFieldsForCPM($createParams);
    if ($rc) {
      return $rc;
    }

    // 操作成功
    return null;
  }

  /**
   * 标准进销存 - 创建，科目1405 - 库存商品，相关的数据库表
   */
  private function createTable_SLN0001_1405($dao, $companyId, $id1405)
  {
    $createParams = [
      "companyId" => $companyId,
      "id" => $id1405,
    ];
    $rc = $dao->createTableForInitFmt($createParams);
    if ($rc) {
      return $rc;
    }

    // 科目1405的仓库字段
    $createParams = [
      "companyId" => $companyId,
      "subjectCode" => "1405",
      "fieldName" => "x_warehouse",
      "fieldType" => 1,
      "voucherInput" => 2,
    ];
    $rc = $dao->createTableColForFMT($createParams);
    if ($rc) {
      return $rc;
    }

    // 科目1405的物料字段
    $createParams = [
      "companyId" => $companyId,
      "subjectCode" => "1405",
      "fieldName" => "x_goods",
      "fieldType" => 1,
      "voucherInput" => 2,
    ];
    $rc = $dao->createTableColForFMT($createParams);
    if ($rc) {
      return $rc;
    }

    // 科目1405的数量金额三栏字段
    $createParams = [
      "companyId" => $companyId,
      "subjectCode" => "1405",
    ];
    $rc = $dao->createDbFieldsForCPM($createParams);
    if ($rc) {
      return $rc;
    }

    // 操作成功
    return null;
  }

  /**
   * 标准进销存 - 创建，科目2202 - 应付账款，相关的数据库表
   */
  private function createTable_SLN0001_2202($dao, $companyId, $id220201, $id220202)
  {
    // 科目220201
    $createParams = [
      "companyId" => $companyId,
      "id" => $id220201,
    ];
    $rc = $dao->createTableForInitFmt($createParams);
    if ($rc) {
      return $rc;
    }

    // 科目220201的供应商字段
    $createParams = [
      "companyId" => $companyId,
      "subjectCode" => "220201",
      "fieldName" => "x_supplier",
      "fieldType" => 1,
      "voucherInput" => 2,
    ];
    $rc = $dao->createTableColForFMT($createParams);
    if ($rc) {
      return $rc;
    }

    // 科目220202
    $createParams = [
      "companyId" => $companyId,
      "id" => $id220202,
    ];
    $rc = $dao->createTableForInitFmt($createParams);
    if ($rc) {
      return $rc;
    }

    // 科目220202的供应商字段
    $createParams = [
      "companyId" => $companyId,
      "subjectCode" => "220202",
      "fieldName" => "x_supplier",
      "fieldType" => 1,
      "voucherInput" => 2,
    ];
    $rc = $dao->createTableColForFMT($createParams);
    if ($rc) {
      return $rc;
    }

    // 操作成功
    return null;
  }

  /**
   * 标准进销存 - 创建，科目4103 - 本年利润，相关的数据库表
   */
  private function createTable_SLN0001_4103($dao, $companyId, $id4103)
  {
    $createParams = [
      "companyId" => $companyId,
      "id" => $id4103,
    ];
    $rc = $dao->createTableForInitFmt($createParams);
    if ($rc) {
      return $rc;
    }

    // 操作成功
    return null;
  }

  /**
   * 标准进销存 - 创建，科目2221 - 应交税费，相关的数据库表，小规模纳税人使用
   */
  private function createTable_SLN0001_2221_samll($dao, $companyId, $id222101)
  {
    // 科目222101
    $createParams = [
      "companyId" => $companyId,
      "id" => $id222101,
    ];
    $rc = $dao->createTableForInitFmt($createParams);
    if ($rc) {
      return $rc;
    }

    // 操作成功
    return null;
  }

  /**
   * 标准进销存 - 创建，科目2221 - 应交税费，相关的数据库表，一般纳税人使用
   */
  private function createTable_SLN0001_2221_general($dao, $companyId, $id22210101, $id22210102, $id222102)
  {

    // 科目22210101
    $createParams = [
      "companyId" => $companyId,
      "id" => $id22210101,
    ];
    $rc = $dao->createTableForInitFmt($createParams);
    if ($rc) {
      return $rc;
    }

    // 科目22210102
    $createParams = [
      "companyId" => $companyId,
      "id" => $id22210102,
    ];
    $rc = $dao->createTableForInitFmt($createParams);
    if ($rc) {
      return $rc;
    }

    // 科目222102
    $createParams = [
      "companyId" => $companyId,
      "id" => $id222102,
    ];
    $rc = $dao->createTableForInitFmt($createParams);
    if ($rc) {
      return $rc;
    }

    // 操作成功
    return null;
  }

  /**
   * 标准进销存 - 创建，科目6001 - 主营业务收入，相关的数据库表
   */
  private function createTable_SLN0001_6001($dao, $companyId, $id6001)
  {
    // 科目6001
    $createParams = [
      "companyId" => $companyId,
      "id" => $id6001,
    ];
    $rc = $dao->createTableForInitFmt($createParams);
    if ($rc) {
      return $rc;
    }

    // 科目6001的客户字段
    $createParams = [
      "companyId" => $companyId,
      "subjectCode" => "6001",
      "fieldName" => "x_customer",
      "fieldType" => 1,
      "voucherInput" => 2,
    ];
    $rc = $dao->createTableColForFMT($createParams);
    if ($rc) {
      return $rc;
    }


    // 操作成功
    return null;
  }

  /**
   * 标准进销存 - 创建，科目6401 - 主营业务收入，相关的数据库表
   */
  private function createTable_SLN0001_6401($dao, $companyId, $id6401)
  {
    $createParams = [
      "companyId" => $companyId,
      "id" => $id6401,
    ];
    $rc = $dao->createTableForInitFmt($createParams);
    if ($rc) {
      return $rc;
    }

    // 科目6401的仓库字段
    $createParams = [
      "companyId" => $companyId,
      "subjectCode" => "6401",
      "fieldName" => "x_warehouse",
      "fieldType" => 1,
      "voucherInput" => 2,
    ];
    $rc = $dao->createTableColForFMT($createParams);
    if ($rc) {
      return $rc;
    }

    // 科目6401的物料字段
    $createParams = [
      "companyId" => $companyId,
      "subjectCode" => "6401",
      "fieldName" => "x_goods",
      "fieldType" => 1,
      "voucherInput" => 2,
    ];
    $rc = $dao->createTableColForFMT($createParams);
    if ($rc) {
      return $rc;
    }

    // 操作成功
    return null;
  }

  /**
   * 标准进销存 - 创建，科目6601 - 销售费用，相关的数据库表
   */
  private function createTable_SLN0001_6601($dao, $companyId, $id6601)
  {
    $createParams = [
      "companyId" => $companyId,
      "id" => $id6601,
    ];
    $rc = $dao->createTableForInitFmt($createParams);
    if ($rc) {
      return $rc;
    }

    // 科目6601的费用项目字段
    $createParams = [
      "companyId" => $companyId,
      "subjectCode" => "6601",
      "fieldName" => "x_expense",
      "fieldType" => 1,
      "voucherInput" => 2,
    ];
    $rc = $dao->createTableColForFMT($createParams);
    if ($rc) {
      return $rc;
    }

    // 操作成功
    return null;
  }

  /**
   * 标准进销存 - 创建，科目6602 - 管理费用，相关的数据库表
   */
  private function createTable_SLN0001_6602($dao, $companyId, $id6602)
  {
    $createParams = [
      "companyId" => $companyId,
      "id" => $id6602,
    ];
    $rc = $dao->createTableForInitFmt($createParams);
    if ($rc) {
      return $rc;
    }

    // 科目6602的费用项目字段
    $createParams = [
      "companyId" => $companyId,
      "subjectCode" => "6602",
      "fieldName" => "x_expense",
      "fieldType" => 1,
      "voucherInput" => 2,
    ];
    $rc = $dao->createTableColForFMT($createParams);
    if ($rc) {
      return $rc;
    }

    // 操作成功
    return null;
  }

  /**
   * 标准进销存 - 创建，科目6603 - 财务费用，相关的数据库表
   */
  private function createTable_SLN0001_6603($dao, $companyId, $id6603)
  {
    $createParams = [
      "companyId" => $companyId,
      "id" => $id6603,
    ];
    $rc = $dao->createTableForInitFmt($createParams);
    if ($rc) {
      return $rc;
    }

    // 科目6603的费用项目字段
    $createParams = [
      "companyId" => $companyId,
      "subjectCode" => "6603",
      "fieldName" => "x_expense",
      "fieldType" => 1,
      "voucherInput" => 2,
    ];
    $rc = $dao->createTableColForFMT($createParams);
    if ($rc) {
      return $rc;
    }

    // 操作成功
    return null;
  }
}
